package com.numbfish.ch16_ex08_r2dbc_web_flux.converter;

import com.numbfish.ch16_ex08_r2dbc_web_flux.pojo.Book;
import com.numbfish.ch16_ex08_r2dbc_web_flux.pojo.Person;
import io.r2dbc.spi.Row;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;

import java.time.LocalDateTime;
@ReadingConverter // 读取数据库时的转换
public class BookConverter implements Converter<Row, Book> {
    @Override
    public Book convert(Row source) {
        Book book = new Book();
        Person person = new Person();
        Integer id = source.get("id", Integer.class);
        Integer authorId = source.get("author_id", Integer.class);
        LocalDateTime publication_date = source.get("publication_date", LocalDateTime.class);
        String title = source.get("title", String.class);
        String name = source.get("name", String.class);
        book.setId(id);
        book.setAuthorId(authorId);
        book.setPublicationDate(publication_date);
        book.setTitle(title);
        person.setName(name);
        book.setPerson(person);
        return book;
    }
}
